package com.tg.app.helper;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.tange.base.toolkit.StringUtils;
import com.tange.xbanner.XBanner;
import com.tange.xbanner.entity.BaseBannerInfo;
import com.tg.app.R;
import com.tg.appcommon.android.ImageUtils;
import com.tg.appcommon.android.ResourcesUtil;
import com.tg.appcommon.android.TGLog;

import java.util.ArrayList;
import java.util.List;

public class DeviceFunHelper {
    private static final String TAG = "BannerHelper";
    private static final String CAMERA_FROM = "camera_from";
    private XBanner mBanner;
    private final List<EntryBean> list = new ArrayList<>();
    private String from = CAMERA_FROM;

    private ImageView intercomView;
    private ImageView audioView;
    private OnBannerClickListener listener;

    private ImageView resolutionView;
    private int resolutionViewResId = 0;
    private int intercomViewResId = 0;
    private int audioViewResId = 0;

    private ImageView speedView;

    public void setParam(List<String> stringList) {
        if (stringList == null || stringList.size() == 0 || stringList.size() == list.size()) {
            return;
        }
        list.clear();
        String val = null;
        for (int i = 0; i < stringList.size(); i++){
            if (StringUtils.isEmpty(val)){
                val = stringList.get(i);
            }else{
                val = val + "," + stringList.get(i);
            }
            if ((i+1) % 4 == 0){
                addDataByTitle(val);
                val = null;
            }
        }
        if (!StringUtils.isEmpty(val)){
            addDataByTitle(val);
        }
    }

    private void addDataByTitle(String text){
        EntryBean bean = new EntryBean();
        bean.title = text;
        this.addData(bean);
    }

    public void setIntercomImageResource(int resId) {
        if (this.intercomView != null) {
            this.intercomView.setImageResource(resId);
        }
        intercomViewResId = resId;
    }
    public void setResolutionImageResource(int resId) {
        TGLog.d("CameraBaseActivity", "this.resolutionView: " + this.resolutionView);
        if (this.resolutionView != null) {
            this.resolutionView.setImageResource(resId);
        }
        resolutionViewResId = resId;
        this.refreshTools();

    }

    public void setAudioImageResource(int resId) {
        setAudioImageResource(resId, true);

    }

    public void setAudioImageResource(int resId, boolean refresh) {
        TGLog.d("CameraBaseActivity", "this.audioView: " + this.audioView);
        if (this.audioView != null) {
            this.audioView.setImageResource(resId);
        }
        audioViewResId = resId;
        if (refresh){
            this.refreshTools();
        }
    }

    public void setSpeedImageResource(int resId) {
        if (this.speedView != null) {
            this.speedView.setImageResource(resId);
        }
    }

    public void setListener(OnBannerClickListener listener){
        this.listener = listener;
    }

    public DeviceFunHelper(){
    }


    public DeviceFunHelper(XBanner banner){
        this.mBanner = banner;
    }

    public void setBanner(XBanner banner){
        this.mBanner = banner;
    }

    public void clear(){
        list.clear();
        mBanner.stopAutoPlay();
        mBanner.setBannerData(list);
    }
    public void initData(List<EntryBean> data){
        mBanner.setAutoPlayAble(true);
        list.clear();
        list.addAll(data);
        mBanner.setBannerData(data);
    }

    public void addData(EntryBean data){
        list.add(data);
        TGLog.d("banner addData size = " + list.size());
    }

    public void refresh(){
        mBanner.setBannerData(list);
        mBanner.setAutoPlayAble(true);
    }

    public void refreshTools(){
        mBanner.setToolsData(list);
        mBanner.setAutoPlayAble(false);
    }


    public int getBannerCurrentItem(){
        return mBanner.getBannerCurrentItem();
    }

    public String getImagePath(int index){
        if (index > -1 && index < list.size()) {
            return list.get(index).url;
        }
        return "";
    }

    public void showBanner(boolean show){
        if (mBanner != null && mBanner.getRealCount() > 0){
            mBanner.setVisibility(show ? View.VISIBLE : View.GONE);
            TGLog.d("banner showBanner size = " + list.size());
        }else{
            mBanner.setVisibility(View.GONE);
        }
    }

    public int getRealCount(){
        if (mBanner != null){
            return mBanner.getRealCount();
        }
        return 0;
    }

    /**
     * 初始化XBanner
     */
    public void initBanner(final Context context) {
        //设置广告图片点击事件
        mBanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, Object model, View view, int position) {
                EntryBean bean = ((EntryBean) model);
                if (bean != null){
                    String url = bean.getXBannerUrl();
                    String title = bean.getXBannerTitle();
                    TGLog.d(TAG, "url = " + url + ", title = " + title);
                    if (StringUtils.equals(from, CAMERA_FROM)){
                        if (!StringUtils.isEmpty(url)){
                            //跳转到广告详情
                            //AdDetailActivity.start(context, url, title);
                        }
                    }else{
                        //跳转到广告详情
                        //AdDetailActivity.start(context, url, title);
                    }
                }

            }
        });
        //加载广告图片
        mBanner.loadImage((banner, model, view, position) -> {
            EntryBean bean = ((EntryBean) model);
            if (bean != null){
                String url = bean.getXBannerUrl();
                String title = bean.getXBannerTitle();
                if (StringUtils.equals(from, CAMERA_FROM)){
                    ImageView imageView1 = view.findViewById(com.tange.xbanner.R.id.iv_image);
                    TextView textView1 = view.findViewById(com.tange.xbanner.R.id.tv_text);
                    ImageView imageView2 = view.findViewById(com.tange.xbanner.R.id.iv_image_1);
                    TextView textView2 = view.findViewById(com.tange.xbanner.R.id.tv_text_1);
                    ImageView imageView3 = view.findViewById(com.tange.xbanner.R.id.iv_image_2);
                    TextView textView3 = view.findViewById(com.tange.xbanner.R.id.tv_text_2);
                    ImageView imageView4 = view.findViewById(com.tange.xbanner.R.id.iv_image_3);
                    TextView textView4 = view.findViewById(com.tange.xbanner.R.id.tv_text_3);
                    List<ImageView> imageViewList = new ArrayList<>();
                    imageViewList.add(imageView1);
                    imageViewList.add(imageView2);
                    imageViewList.add(imageView3);
                    imageViewList.add(imageView4);
                    List<TextView> textViews = new ArrayList<>();
                    textViews.add(textView1);
                    textViews.add(textView2);
                    textViews.add(textView3);
                    textViews.add(textView4);
                    int flag = bean.getFlag();
                    if (flag == 0){
                        String[] list = title.split(",");
                        for(int i = 0; i < list.length; i++){
                            setImage(imageViewList.get(i), textViews.get(i), list[i]);

                        }
                    }
                }else {
                    if (!StringUtils.isEmpty(url)){
                        ImageUtils.loadImage(context, (ImageView)view, url,false,false);
                    }
                }
            }
        });
        //代码设置框架占位图，也可在布局中设置
        mBanner.setBannerPlaceholderImg(R.drawable.shape_dialog_playback_time, ImageView.ScaleType.CENTER_CROP);
    }


    private void setImage(final ImageView imageView, final TextView textView, String title){
        if (StringUtils.equals(title, ResourcesUtil.getString(R.string.fullscreen))){
            imageView.setImageResource(R.drawable.ic_tange_live_fullscreen);
        }else if (StringUtils.equals(title, ResourcesUtil.getString(R.string.clarity))){
            resolutionView = imageView;
            imageView.setImageResource(resolutionViewResId == 0? R.drawable.ic_tange_live_hd : resolutionViewResId);
        }else if (StringUtils.equals(title, ResourcesUtil.getString(R.string.event_sound))){
            audioView = imageView;
            imageView.setImageResource(audioViewResId == 0? R.drawable.ic_tange_live_soundoff : audioViewResId);
        }else if (StringUtils.equals(title, ResourcesUtil.getString(R.string.txt_switch))){
            imageView.setImageResource(R.drawable.ic_tange_live_control);
        }else if (StringUtils.equals(title, ResourcesUtil.getString(R.string.record_screen))) {
            imageView.setImageResource(R.drawable.ic_tange_live_video);
        }else if (StringUtils.equals(title, ResourcesUtil.getString(R.string.speed))) {
            speedView = imageView;
            imageView.setImageResource(R.drawable.ic_tange_live_speed);
        }else if (StringUtils.equals(title, ResourcesUtil.getString(R.string.screen_shot))) {
            imageView.setImageResource(R.drawable.ic_tange_live_screenshot);
        } if (StringUtils.equals(title, ResourcesUtil.getString(R.string.click_to_talk))) {
            intercomView = imageView;
            imageView.setImageResource(intercomViewResId == 0? R.drawable.ic_tange_live_mic : intercomViewResId);
        }
        textView.setText(title);

        imageView.setOnClickListener(v -> {
            if (DeviceFunHelper.this.listener != null){
                DeviceFunHelper.this.listener.onBannerClick(imageView, title);
            }
        });

        textView.setOnClickListener(v -> {
            if (DeviceFunHelper.this.listener != null){
                DeviceFunHelper.this.listener.onBannerClick(imageView, title);
            }
        });
    }
    public static class EntryBean implements BaseBannerInfo {
        private String title;
        private String url;
        private int flag = 0;
        private int getFlag(){
            return flag;
        }

        public void setTitle(String title){
            this.title = title;
        }

        public void setUrl(String url){
            this.url = url;
        }

        @Override
        public String getXBannerUrl() {
            return url;
        }

        @Override
        public String getXBannerTitle() {
            return title;
        }

    }

    public interface OnBannerClickListener {
        void onBannerClick(View view, String title);
    }
}
